package com.saycode.portal.article.service.impl;

import com.saycode.common.Constant;
import com.saycode.common.MethodUtil;
import com.saycode.common.enums.StatusEnum;
import com.saycode.common.beans.Article;
import com.saycode.common.beans.ArticleContent;
import com.saycode.common.dao.ArticleContentRepository;
import com.saycode.common.dao.ArticleRepository;
import com.saycode.portal.article.service.ArticleService;
import org.codehaus.groovy.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * Created by zhangyanhui on 2017/6/14.
 */
@Service("portal/articleService")
@RequestMapping("portal/articleService")
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;
    @Autowired
    private ArticleContentRepository articleContentRepository;
    public List<Article> findAll(){
        return articleRepository.findAll();
    }

    @Override
    public Article findById(Long id) {
        return articleRepository.findOne(id);
    }

    @Override
    public ArticleContent findContentById(Long id) {
        return articleContentRepository.findOne(id);
    }

    public Page<Article> findByStatusAndContentType(StatusEnum statusEnum,String contentType, int pageNum){
        PageRequest request = new PageRequest(pageNum-1, Constant.PAGE_NUM,new Sort(Sort.Direction.DESC,"updateDate"));
        Page<Article> page = null;
        if(MethodUtil.isNull(contentType)){
            page = articleRepository.findByStatus(statusEnum.getKey(),request);
        }else{
            page = articleRepository.findByStatusAndContentType(statusEnum.getKey(),contentType,request);
        }
        return page;
    }
}
